﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Reflection;
using System.Threading;
using QASampling.BusinessObject;
using QASampling.WSStellent;

namespace QASampling.UI
{
    public partial class frmJob : BaseFormWithHeader
    {
        public frmJob()
        {
                InitializeComponent();
        }

        private void PopulateDataGrid()
        {
            qaBaseDataGrid1.Rows.Clear();
            IList<QAJob> lstJob = QAController.GetJobs(QAController.Username);
            foreach (QAJob job in lstJob)
            {
                string waktuPengecekan = job.StatusAkhir.Length == 0 ? "" : job.WaktuPengecekan.ToString("dd MMM yyyy HH:mm:ss");
                object[] values = { job.DocId, job.Barcode, job.NamaPTKP, job.Npwp, job.MasaPajak, 
                                      job.WaktuPenugasan.ToString("dd MMM yyyy HH:mm:ss"), job.StatusAsal, job.StatusAkhir, waktuPengecekan, job.Catatan, job.JenisSPT};
                qaBaseDataGrid1.Rows.Add(values);
            }

            //--added by Vincent 20130110 for row number--
            //dgvJob.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders; 
            /*int rowNumber = 1;
            foreach (DataGridViewRow row in qaBaseDataGrid1.Rows)
            {
                if (row.IsNewRow) continue;
                row.HeaderCell.Value = "" + rowNumber;
                rowNumber = rowNumber + 1;
            }
            qaBaseDataGrid1.RowHeadersWidth = 50;*/
            //dgvJob.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);  
        }

        private void btnRefresh_Click_1(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                PopulateDataGrid();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void qaBaseDataGrid1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            OpenForm();
        }

        private void OpenForm()
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                if (qaBaseDataGrid1.SelectedRows.Count == 0) return;

                string docId = qaBaseDataGrid1.SelectedRows[0].Cells[0].Value.ToString();
                string barcode = qaBaseDataGrid1.SelectedRows[0].Cells[1].Value.ToString();

                QAJob job = QAController.GetJobByDocIdAndBarcode(docId, barcode);
                QAController.Job = job;
                //QAController.DocId = docId;

                switch (job.JenisSPT)
                {
                    case "1111":
                        using (FormSPT1111 f = new FormSPT1111())
                            f.ShowDialog(this);
                        break;
                    case "1111DM":
                        using (FormSPT1111DM f = new FormSPT1111DM())
                            f.ShowDialog(this);
                        break;
                    case "1770":
                        using (FormSPT1770 f = new FormSPT1770())
                            f.ShowDialog(this);
                        break;
                    case "1770S":
                        using (FormSPT1770S f = new FormSPT1770S())
                            f.ShowDialog(this);
                        break;
                    case "1770SS":
                        using (FormSPT1770SS f = new FormSPT1770SS())
                            f.ShowDialog(this);
                        break;
                    case "1107":
                        using (FormSPT1107 f = new FormSPT1107())
                            f.ShowDialog(this);
                        break;
                    case "1108":
                        using (FormSPT1108 f = new FormSPT1108())
                            f.ShowDialog(this);
                        break;
                    case "1771":
                        using (FormSPT1771 f = new FormSPT1771())
                            f.ShowDialog(this);
                        break;
                    case "1721":
                        using (FormSPT1721 f = new FormSPT1721())
                            f.ShowDialog(this);
                        break;
                }
                /*Type t = Type.GetType("QASampling.UI.FormSPT" + job.JenisSPT, true);
                MethodInfo method
                     = t.GetMethod("ShowDialog", new Type[0]);
                object dialog = Activator.CreateInstance(t);
                method.Invoke(dialog, null);*/

                PopulateDataGrid();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void btnBuka_Click(object sender, EventArgs e)
        {
            if (qaBaseDataGrid1.SelectedRows.Count == 0)
            {
                MessageBox.Show("Tidak ada data yang dipilih", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            //else if added by Vincent 20130205 for handling exception open in order to copy all grid rows to excel--------
            else if (qaBaseDataGrid1.SelectedRows.Count > 1)
            {
                MessageBox.Show("Pilih salah satu yang akan dibuka", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            //-------------------------------------------------------------------------------------------------------------
            else
            {
                OpenForm();
            }
        }

        private void btnDetail_Click(object sender, EventArgs e)
        {
            try
            {
                if (qaBaseDataGrid1.SelectedRows.Count == 0)
                {
                    MessageBox.Show("Tidak ada data yang dipilih", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                //else if added by Vincent 20130205 for handling exception open in order to copy all grid rows to excel--------
                else if (qaBaseDataGrid1.SelectedRows.Count > 1)
                {
                    MessageBox.Show("Pilih salah satu yang akan dibuka", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                //-------------------------------------------------------------------------------------------------------------

                if (qaBaseDataGrid1.SelectedRows.Count == 0) return;

                string docId = qaBaseDataGrid1.SelectedRows[0].Cells[0].Value.ToString();
                string barcode = qaBaseDataGrid1.SelectedRows[0].Cells[1].Value.ToString();

                QAJob job = QAController.GetJobByDocIdAndBarcode(docId, barcode);

                using (frmJobDetails f = new frmJobDetails())
                {
                    //f.lblFormulirName.Text = "Formulir " + job.JenisSPT;
                    f.txtDocId.Text = job.DocId;
                    f.txtNama.Text = job.NamaPTKP;
                    f.txtNPWP.Text = job.Npwp;
                    f.txtMasaPajak.Text = job.MasaPajak;
                    f.txtWaktuPenugasan.Text = job.WaktuPenugasan.ToString("dd MMM yyyy HH:mm:ss");
                    f.txtStatusAwal.Text = job.StatusAsal;
                    f.txtWaktuPengecekan.Text = job.WaktuPengecekan.ToString("dd MMM yyyy HH:mm:ss");
                    f.txtStatusAkhir.Text = job.StatusAkhir;
                    f.txtCatatan.Text = job.Catatan;
                    f.txtPetugas.Text = job.DitugaskanKepada;
                    f.txtBarcode.Text = job.Barcode;
                    f.ShowDialog(this);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void frmJob_Load(object sender, EventArgs e)
        {
            this.Hide();
            using (frmLogin f = new frmLogin())
            {
                System.Windows.Forms.DialogResult result = f.ShowDialog(this);
                if (result == System.Windows.Forms.DialogResult.Cancel)
                {
                    Application.Exit();
                    return;
                }
                else
                    lblUsername.Text = QAController.Username;
            }
            this.lblFormulirName.Text = "Quality Assurance " + QAController.TipeSPT.ToString();
            if (QAController.TipeSPT == TipeSPT.Balance)
                QAController.ReadOnly = true;
            else
                QAController.ReadOnly = false;
            btnRefresh_Click_1(sender, e);           
            this.Show();
        }

        private void btnLogout_Click(object sender, EventArgs e)
        {
            DialogResult jawab = MessageBox.Show(this,"Apakah anda yakin mau logout ?","Logout",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
            if (jawab == DialogResult.OK)
            {
                QAController.UbahStatusLoginPetugas(0);
                frmJob_Load(sender, e);
            }
        }

        private void frmJob_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(QAController.Username))
                    QAController.UbahStatusLoginPetugas(0);
            }
            catch (Exception)
            {
                Application.Exit();
            }
        }

        private void btnLastJob_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                QAJob job = QAController.GetLastJob();
                if (job == null)
                {
                    MessageBox.Show("Tidak ada SPT yang dikerjakan hari ini", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                QAController.Job = job;

                switch (job.JenisSPT)
                {
                    case "1111":
                        using (FormSPT1111 f = new FormSPT1111())
                            f.ShowDialog(this);
                        break;
                    case "1111DM":
                        using (FormSPT1111DM f = new FormSPT1111DM())
                            f.ShowDialog(this);
                        break;
                    case "1770":
                        using (FormSPT1770 f = new FormSPT1770())
                            f.ShowDialog(this);
                        break;
                    case "1770S":
                        using (FormSPT1770S f = new FormSPT1770S())
                            f.ShowDialog(this);
                        break;
                    case "1770SS":
                        using (FormSPT1770SS f = new FormSPT1770SS())
                            f.ShowDialog(this);
                        break;
                    case "1107":
                        using (FormSPT1107 f = new FormSPT1107())
                            f.ShowDialog(this);
                        break;
                    case "1108":
                        using (FormSPT1108 f = new FormSPT1108())
                            f.ShowDialog(this);
                        break;
                    case "1771":
                        using (FormSPT1771 f = new FormSPT1771())
                            f.ShowDialog(this);
                        break;
                    case "1721":
                        using (FormSPT1721 f = new FormSPT1721())
                            f.ShowDialog(this);
                        break;
                }
                /*Type t = Type.GetType("QASampling.UI.FormSPT" + job.JenisSPT, true);
                MethodInfo method
                     = t.GetMethod("ShowDialog", new Type[0]);
                object dialog = Activator.CreateInstance(t);
                method.Invoke(dialog, null);*/

               // SplashScreen.SplashScreen.ShowSplashScreen();
               // Type t = Type.GetType("FormSPT" + job.JenisSPT, true);
                //MethodInfo method
                //     = t.GetMethod("ShowDialog", BindingFlags.Static | BindingFlags.Public);

               // method.Invoke(null, null);
               /* if (job.JenisSPT == "1111")
                {
                   

                    //FormSPT1111 f = new FormSPT1111();
                    //f.ShowDialog(this);
                }
                else if (job.JenisSPT == "1111DM")
                {
                    FormSPT1111DM f = new FormSPT1111DM();
                    f.ShowDialog(this);
                }*/

                PopulateDataGrid();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        protected override bool ProcessCmdKey(ref Message message, Keys keys)
        {
            switch (keys)
            {
                case Keys.F5:
                    //Process action here.
                    btnRefresh_Click_1(new object(), new EventArgs());
                    break;
                case Keys.D | Keys.Control:                    
                    btnDetail_Click(new object(), new EventArgs());
                    break;
                case Keys.B | Keys.Control:
                    btnBuka_Click(new object(), new EventArgs());
                    break;
                case Keys.S | Keys.Control:
                    btnLastJob_Click(new object(), new EventArgs());
                    break;
                //added by Vincent 20130205 for shortcut logout
                case Keys.L | Keys.Control:
                    btnLogout.PerformClick();
                    break;
            }

            return false;
        }
    }
}
